package strings;

/**
 * @author pengfei.hpf
 * @date 2020/3/14
 * @verdion 1.0.0
 */
public class Zigzagconversion {
    public String convert(String s, int numRows){
        if(s == null || s.length() == 0 || numRows <= 1){
            return s;
        }
        int len = s.length();
        //TODO: 注意长度计算. 如果numRows很大 而s.length() 很小, 列至少为1.
        char[][] chars = new char[numRows][len];
        int i = 0;
        char[] cs = s.toCharArray();
        int r = 0;
        int col = 0;
        while(i < len){
            for(; r < numRows && i < len; r ++){
                chars[r][col] = cs[i];
                i++;
            }
            r = numRows - 2;
            col ++;
            for(; r > 0 && i < len; col ++, r --){
                chars[r][col] = cs[i];
                i++;
            }
        }
        StringBuilder builder = new StringBuilder();
        for(int m = 0; m < chars.length; m++){
            for(int n = 0; n < chars[0].length; n ++){
                if(chars[m][n] != 0){
                    builder.append(chars[m][n]);
                }
            }
        }
        return builder.toString();
    }
    public static void main(String[] args){
        System.out.println(new Zigzagconversion().convert("asdfa", 3));
    }
}
